package es.cmesa.epersistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.GregorianCalendar;

public class RepositorioMesa {
	private Connection con;
	
	private int numMesa;
	private int numConta;
	
	public RepositorioMesa(int numeroMesa){
		this.con = new ConnectionFactory().getConnection();
		this.numMesa = numeroMesa;
	}
	
	public RepositorioMesa(int numMesa, int numConta){
		this.numMesa = numMesa;
		this.numConta = numConta;
	}
	
	public int iniciarConta(){
		String data = pegaData();
		try{
			String sql = "insert into conta (data) values (?)";
			PreparedStatement stmt = con.prepareStatement(sql);
			stmt.setString(1, data);
			
			System.out.println("chegou na persistencia");
			
			stmt.execute();
			stmt.close();
			
			numConta = pegarNumeroConta();
			
		} catch(SQLException e){
			throw new RuntimeException(e);
		}
		return 1;
	}
	
	public int pegarNumeroConta(){
		
		
		return 1;
	}
	
	public void pedido(int id, int numMesa){
		try{
			String sql = "insert into pedido (idproduto, numeromesa) values (?,?)";
			PreparedStatement stmt = con.prepareStatement(sql);
			stmt.setInt(1, id);
			stmt.setInt(2, numMesa);
			
			stmt.execute();
			stmt.close();
			
			numConta = pegarNumeroConta();
			
		} catch(SQLException e){
			throw new RuntimeException(e);
		}
	}
	
	public void pedidoLogado(int id, int numMesa, String cpf){
		try{
			String sql = "insert into pedido (idproduto, numeromesa, cpfcliente) values (?,?,?)";
			PreparedStatement stmt = con.prepareStatement(sql);
			stmt.setInt(1, id);
			stmt.setInt(2, numMesa);
			stmt.setString(3, cpf);
			
			System.out.println("chegou na persistencia");
			
			stmt.execute();
			stmt.close();
			
			numConta = pegarNumeroConta();
			
		} catch(SQLException e){
			throw new RuntimeException(e);
		}
	}
	
	public void login(int numMesa, int numConta){
		
	}
	
	public void chamarGarcon(){
		
	}
	
	public void encerraConta(boolean dezPorCento,int numConta){
		
	}
	
	public String pegaData(){
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setLenient(false);
		
		int dia = calendar.get(GregorianCalendar.DAY_OF_MONTH);  
		int mes = calendar.get(GregorianCalendar.MONTH); 
		int ano = calendar.get(GregorianCalendar.YEAR);
		
		mes++;
		
		String data = dia + "/" + mes + "/" + ano;
		
		return data;
	}
}
